<?php

/**
 * @file
 * Administration page callbacks for the barcode module.
 */

function barcode_admin_settings() {
  // Barcode Encoding Type Generation Area --
  $form['barcodetype'] = array(
    '#type' => 'fieldset',
    '#title' => t('Barcode Encoding Type'),
  );

  $form['barcodetype']['type'] = array(
     '#type' => 'radios',
     '#title' => t('Encoding types'),
     '#description' => t('Select the barcode encoding type which will be used for barcode generation.'),
     '#options' => array(t('EAN 8'), t('EAN 13'), t('standard 2 of 5 (industrial)'), t('interleaved 2 of 5'), t('code 11'), t('code 39'), t('code 93'), t('code 128'), t('codabar'), t('MSI'), t('Datamatrix (ASCII + extended)')),
     '#default_value' =>  variable_get('type', 5), // Default to case 39
  );
  //---EOA: End of Area

  // Miscellaneous Settings Area
  $form['misc'] = array(
     '#type' => 'fieldset',
     '#title' => t('Miscellaneous'),
  );

  $form['misc']['background'] = array(
    '#type' => 'textfield',
    '#title' => t('Background Color'),
    '#size' => 8,
    '#maxlength' => 7,
    '#default_value' => variable_get('background', '#FFFFFF'),
    '#description' => 'Set the background color of barcode. Value of color should be in hexadecimal form e.g #ffffff',
  );

  $form['misc']['barcolor'] = array(
    '#type' => 'textfield',
    '#title' => t('Barcolor'),
    '#size' => 8,
    '#maxlength' => 7,
    '#default_value' => variable_get('barcolor', '#000000'),
    '#description' => 'Set the barcolor of barcode. Value of color should be in hexadecimal form e.g #ffffff',
  );

  $form['misc']['width'] = array(
    '#type' => 'textfield',
    '#title' => t('Bar Width [In pixel]'),
    '#size' => 4,
    '#maxlength' => 4,
    '#default_value' =>  variable_get('width', '1'),
    '#description' => 'Set the barcode width. Value of width should be in pixels e.g  2, 3 (do not specify px)',
  );

  $form['misc']['height'] = array(
    '#type' => 'textfield',
    '#title' => t('Bar Height [In pixel]'),
    '#size' => 4,
    '#maxlength' => 4,
    '#default_value' =>  variable_get('height', '60'),
    '#description' => 'Set the barcode height. Value of height should be in pixel e.g  2, 3 (do not specify px)',
  );
  // --EOA

  // Barcode Image Generation Area
  $form['generate'] = array(
    '#type' => 'fieldset',
    '#title' => t('Barcode Image Generation'),
    '#tree' => FALSE,
  );

  $form['generate']['barcodevalue'] = array(
    '#type' => 'radios',
    '#title' => t('Select the barcode value'),
    '#description' => t('Select the barcode value which will be used for barcode generation.'),
    '#options' => array(
      t('User name [only allowed for case 39,case 93,case 128 & datamatrix encoding]'),
      t('Random String'),
    ),
    '#default_value' =>  variable_get('barcodevalue', 1),  // Default to random string
  );

  $form['generate']['showvalue'] = array(
    '#type' => 'checkbox',
    '#title' => t('Show value below the barcode'),
    '#default_value' => variable_get('showvalue', 1),
    '#tree' => FALSE,
    '#description' => t('Check here if you want to show code below the barcode.'),
  );

  $form['generate']['position'] = array(
    '#type' => 'fieldset',
    '#title' => t('Barcode position [These option will only work if custom barimage is specified]'),
  );

  $form['generate']['position']['barcode_center_x'] = array(
    '#type' => 'textfield',
    '#title' => t('X-cordinate of barcode center [In pixel]'),
    '#size' => 6,
    '#maxlength' => 5,
    '#default_value' =>  variable_get('barcode_center_x', 100),
    '#description' => 'Set the X-cordinate of barcode center. Value should be in pixel e.g  100,200 [do not specify px]',
  );

  $form['generate']['position']['barcode_center_y'] = array(
    '#type' => 'textfield',
    '#title' => t('Y-cordinate of barcode center [In pixel]'),
    '#size' => 6,
    '#maxlength' => 5,
    '#default_value' =>  variable_get('barcode_center_y', 100),
    '#description' => 'Set the Y-cordinate of barcode center. Value should be in pixel e.g  100,200 [do not specify px]',
  );

   //--EOA

   // Image  Uploading Area
  $form['barimage'] = array(
    '#type' => 'fieldset',
    '#title' => t('Barcode image settings'),
    '#description' => t('If toggled on, the following image will be displayed.'),
    '#suffix' => '<div><b>Note: </b> if "Use the default barimage" option is not selected and custom barimage path is not specified then only barcode will be generated. No background image will be used.</div>',
  );

  $form['barimage']['default_barimage'] = array(
    '#type' => 'checkbox',
    '#title' => t('Use the default barimage'),
    '#default_value' => variable_get('default_barimage', 0),
    '#tree' => FALSE,
    '#description' => t('Check here if you want to use the image supplied with it.'),
  );
  $barimage_path = variable_get('barimage_path', '');
  if ($barimage_path != '' && file_exists($barimage_path)) {
      // then nothing to do
  }
  else {
    $barimage_path = NULL;
  }

  $form['barimage']['barimage_path'] = array(
    '#type' => 'textfield',
    '#title' => t('Path to custom bar image'),
    '#default_value' => $barimage_path,
    '#description' => t('The path to the file you would like to use as your bar image file instead of the default bar image.'),
  );
  $form['barimage']['barimage_upload'] = array(
    '#type' => 'file',
    '#title' => t('Upload bar image [only .jpeg extension]'),
    '#maxlength' => 40,
    '#description' => t("If you don't have direct file access to the server, use this field to upload your image."),
  );
  //define your limits for the submission here
  $validators = array(
    'file_validate_is_image' => array(),
    'file_validate_extensions' => array('jpeg'),
    'file_validate_image_resolution' => array(variable_get('user_picture_dimensions', '1280x800')),
    'file_validate_size' => array(variable_get('user_picture_file_size', '30') * 1024),
  );

  // Check for a new uploaded barimage, and use that instead.
  if ($file = file_save_upload('barimage_upload', $validators)) {
    $parts = pathinfo($file->filename);
    $filename = 'barimage.' . $parts['extension'];

    // The image was saved using file_save_upload() and was added to the
    // files table as a temporary file. We'll make a copy and let the garbage
    // collector delete the original upload.
    if (file_copy($file, $filename, FILE_EXISTS_REPLACE)) {
      $_POST['default_barimage'] = 0;
      $_POST['barimage_path'] = $file->filepath;
    }
  }
  $form['#attributes'] = array('enctype' => 'multipart/form-data');

  return system_settings_form($form);
}

/**
 *
 * @param type $form
 * @param type $form_state
 * Validate the configuration form.
 */
function barcode_admin_settings_validate($form, $form_state) {
  $background = $form_state['values']['background'];
  $barcolor = $form_state['values']['barcolor'];
  $width  = $form_state['values']['width'];
  $height = $form_state['values']['height'];
  $barcode_center_x = $form_state['values']['barcode_center_x'];
  $barcode_center_y = $form_state['values']['barcode_center_y'];

  if (!ereg("^\#([a-fA-F0-9]{6}|[a-fA-F0-9]{3})$", $background)) {
    form_set_error('background', t('Set background color in valid hexadecimal RGB form.'));
  }

  if (!ereg("^\#([a-fA-F0-9]{6}|[a-fA-F0-9]{3})$", $barcolor)) {
    form_set_error('barcolor', t('Set bar color in valid hexadecimal RGB form.'));
  }

  if (!is_numeric($width)) {
    form_set_error('width', t('Please enter a number for width.'));
  }

  if (!is_numeric($height)) {
    form_set_error('height', t('Please enter a number for height.'));
  }

  if (!is_numeric($barcode_center_x)) {
    form_set_error('barcode_center_x', t('Please enter a number for X-cordinate fo barcode center.'));
  }

  if (!is_numeric($barcode_center_y)) {
    form_set_error('barcode_center_y', t('Please enter a number for Y-cordinate fo barcode center.'));
  }
}